package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.Command;
import model.IAction;
import service.GlobalService;
import utilities.GameResource;
import utilities.TField;
import font.BitmapFont;

public class ChangePassScr extends Screen {

    static ChangePassScr instance;

    public static ChangePassScr gI() {
        if (instance == null) {
            instance = new ChangePassScr();
        }
        
        instance.clean();
        return instance;
    }
    //oldpass, newpass, newpassagane
    private TField listTextField[];
    private String listText[] = {GameResource.oldPass, GameResource.newPass, GameResource.typePass};
    int wC, xC;
    int focus = 0;
    Command cmdClose;
    Command cmdFindFriend;
    Command cmdSelect;
    public Screen lastScr;

    private void clean() {
        int a = listTextField.length;
        for (int i = 0; i < a; i++) {
            TField txt = listTextField[i];
            txt.setText("");
        }
    }
    //show screen

    public void switchToMe() {
        lastScr = GameCanvas.currentScreen;
        m_cmdRight = listTextField[0].cmdClear;
        m_cmdLeft = cmdClose;
        m_cmdCenter = cmdFindFriend;
        super.switchToMe();
    }

    public ChangePassScr() {
        m_width = GameCanvas.w3d4;
        m_left = (GameCanvas.w - m_width) / 2;

        m_height = 4 * (ITEM_HEIGHT + 2) + 2 * 5 + 30;
        m_top = (GameCanvas.h - m_height) / 2;

        wC = m_width - 100;
        xC = m_left + 70;

        int y = m_top + 15 + (ITEM_HEIGHT + 2) / 2;
        listTextField = new TField[3];
        for (int i = 0; i < 3; i++) {
            listTextField[i] = new TField();
            listTextField[i].y = y;
            listTextField[i].width = wC;
            listTextField[i].height = ITEM_HEIGHT + 2;
            listTextField[i].setIputType(TField.INPUT_TYPE_PASSWORD);
            listTextField[i].isFocus = false;
            y += ITEM_HEIGHT + 4;
        }
        listTextField[0].isFocus = true;

        cmdFindFriend = new Command(GameResource.changePass, new IAction() {

            public void perform() {
            	
              final  String oldPass = listTextField[0].getText().trim();
              final  String newPass = listTextField[1].getText().trim();
              final  String retypePass = listTextField[2].getText().trim();
                if (newPass.length() < 5) {
                    GameCanvas.startOKDlg("Mật khẩu phải nhiều hơn 5 kí tự");
                    return;
                }
                if (!newPass.equals(retypePass)) {
                    GameCanvas.startOKDlg("Mật khẩu nhập lại không khớp");
                    return;
                }
                
                   GameCanvas.startOKDlg("Bạn chắc chắn muốn thay đổi mật khẩu ?. Một tin nhắn kết quả sẽ trả về cho bạn.", new IAction() {
                    public void perform() {
                    	System.out.println(NuoiThuMidlet.m_myPlayerInfo.m_UserName);
                    	GlobalService.gI().doChangePass(NuoiThuMidlet.m_myPlayerInfo.m_UserName, oldPass, newPass);
                        close();
                        GameCanvas.startWaitDlg(GameResource.plzWait);
                    }
                });
                
            }
        });

        cmdClose = new Command(GameResource.close, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                close();
                System.gc();
            }
        });

        cmdSelect = new Command(GameResource.select, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                Vector menu = new Vector();
                Command maleCmd = new Command(GameResource.male, new IAction() {

                    public void perform() {
                        // TODO Auto-generated method stub
                        listTextField[3].setText(GameResource.male);
                    }
                });
                menu.addElement(maleCmd);

                Command femaleCmd = new Command(GameResource.female, new IAction() {

                    public void perform() {
                        // TODO Auto-generated method stub
                        listTextField[3].setText(GameResource.female);
                    }
                });
                menu.addElement(femaleCmd);

                GameCanvas.menu.startAt(menu, 1);
            }
        });
    }

    public void close() {
        m_cmdCenter = null;
        m_cmdRight = null;
        m_cmdLeft = null;
        GameCanvas.cameraList.close();

        lastScr.switchToMe();
    }

    public void update() {
        lastScr.update();
        for (int i = 0; i < 3; i++) {
            TField txt = listTextField[i];
            if (txt.x != xC) {
                txt.x += (xC - txt.x + 1) >> 1;
            }
            txt.update();
        }
    }

    public void updateKey() {
    	boolean checkFocus = false;
        if (GameCanvas.isPointerClick) {
            int a = listTextField.length;
            for (int i = 0; i < a; i++) {
                TField txt = listTextField[i];
                if (GameCanvas.isPointer(txt.x, txt.y, txt.width, txt.height)) {
                    focus = i;
                    txt.isFocus = true;
                    m_cmdRight = txt.cmdClear;
                    
                    checkFocus = true;
                    
                    break;
                }
            }
        }


        if (GameCanvas.keyPressed[2]) {
            focus--;
            if (focus < 0) {
                focus = listTextField.length - 1;
            }

        } else if (GameCanvas.keyPressed[8]) {
            focus++;
            if (focus >= listTextField.length) {
                focus = 0;
            }
        }

        if (GameCanvas.keyPressed[2] || GameCanvas.keyPressed[8] || checkFocus) {
            GameCanvas.clearKeyPressed();
            int b = listTextField.length;
            for (int i = 0; i < b; i++) {
                TField txt = listTextField[i];
                if (focus == i) {
                    txt.isFocus = true;
                    m_cmdRight = txt.cmdClear;
                }
                txt.isFocus = (focus == i);
            }
        }        
        
        super.updateKey();
    }

    public void keyPress(int keyCode) {
        int a = listTextField.length;
        for (int i = 0; i < a; i++) {
            if (listTextField[i].isFocus) {
                listTextField[i].keyPressed(keyCode);
            }
        }

        super.keyPress(keyCode);
    }

    public void paint(Graphics g) {
        lastScr.paint(g);
        g.translate(-g.getTranslateX(), -g.getTranslateY());
        g.setClip(m_left, m_top, m_width, m_height);
        PaintPopup.paintRoundRect(g, m_left, m_top, m_width, m_height, 0x3c8dc0);
        
        BitmapFont.drawBoldFont(g, GameResource.changePass, m_left + m_width / 2, m_top + 5, 0xffffff, Graphics.HCENTER);
        int a = listText.length;
        for (int i = 0; i < a; i++) {
            BitmapFont.drawNormalFont(g, listText[i], m_left + 10, listTextField[i].y + 4, 0x3a001e, Graphics.LEFT);
        }
        int b = listTextField.length;
        for (int i = 0; i < b; i++) {
            listTextField[i].paint(g);
        }

        super.paint(g);
    }
    
    public void paintMarqueeMsg(Graphics g){
	}
}
